<template>
    <div class="table-container pull-chheight">
        <basic-container>
            <avue-crud :option="tableOption"
                       v-model="mainBeanModel"
                       :data="tableData"
                       :table-loading="tableLoading"
                       :row-class-name="tableRowClassName"
                       :page="pageBeanModel"
                       ref="crud"
                       @row-save="handleSave"
                       @row-update="handleUpdate"
                       @row-del="handleDel"
                       @refresh-change="handlerefreshChange"
                       @current-change="handlePageCurrentChange"
                       @size-change="handlePageSizeChange"
                       @search-change="handleSearchChange"
                       @selection-change="handleSelectionChange">
                <template slot="menuLeft">
                    <el-button type="primary"
                               icon="el-icon-edit"
                               size="small"
                               v-if="permission.antStock_update"
                               @click="handleRowEdit">{{ $t('commHandle.editBtnTxt')}}</el-button>
                    <el-button type="primary"
                               icon="el-icon-delete"
                               size="small"
                               v-if="permission.antStock_delete"
                               @click="handleRowDel">{{ $t('commHandle.deleteBtnTxt')}}</el-button>
                </template>
            </avue-crud>
        </basic-container>

    </div>
</template>
<script>
    import { mapGetters } from 'vuex';
    import tableOption from '@/views/ant/antStock/antStockOption';
    import { parseTime } from '@/util/filters'
    import { validatenull } from '@/util/validate';
    import { readAntStockByPage,addAntStock,editAntStock,delAntStock } from '@/views/ant/antStock/antStockApi'

    export default {
        name: 'table',
        data () {
            return {
                tableSearch: {},
                tableOption: tableOption, //表格设置属性
                tableData: [], //表格的数据
                tableRow: {},
                tableLoading: false,
                allSysMenuTreeData: [],//系统所有菜单树数据
                mainBeanModel: {},
                pageBeanModel: {
                    total: 0, //总页数
                    currentPage: 1, //当前页数
                    pageSize: 10, //每页显示多少条
                },
                antStockMenuTreeProps: {
                    children: 'children',
                    label: 'menuName'
                },
                antStockMenuParamBean: {
                    showDialog: false,
                    antStockId:-1,
                    selectedItems: [],
                },
            };
        },
        created () {
            this.handleList();
        },
        watch: {},
        mounted () { },
        computed: {
                ...mapGetters(['permission', 'menuAll']),
    },
    props: [],
            methods: {
        /**
         * @title 获取数据
         * @detail 赋值为tableData表格即可
         *
         **/
        handleList () {
            this.tableLoading = true;
            let searchParam = this.tableSearch;
            let pageParams = {pageIndex:this.pageBeanModel.currentPage,pageSize:this.pageBeanModel.pageSize}
            let allParams = Object.assign(searchParam, pageParams);
            readAntStockByPage(allParams).then(res => {
                const data = res.data;
            this.tableData = data.data;
            this.pageBeanModel.total = data.total;
            this.pageBeanModel.currentPage = data.current;
            this.tableLoading = false;
        });
        },

        /**
         * @title 数据添加
         * @param row 为当前的数据
         * @param done 为表单关闭函数
         *
         **/
        handleSave (row, done,loading) {
            addAntStock(row).then(() => {
            let addSuccTip = this.$t("commHandle.addSuccTip");
            this.$message.success(addSuccTip);
            done();
            this.handleList();
        }).catch(() => {
                loading();
        })
        },
        handleRowEdit () {
            let row = this.getSelectOneData();
            if(!row)return;
            this.handleEdit(row, -1);
        },

        handleEdit (row, index) {
            this.$refs.crud.rowEdit(row, index);
        },
        /**
         * @title 数据更新
         * @param row 为当前的数据
         * @param index 为当前更新数据的行数
         * @param done 为表单关闭函数
         * @param loading 为表单关闭处理中的函数
         **/
        handleUpdate (row,index,done,loading) {
            console.info(index);
            editAntStock(row).then(()=> {
                let editSuccTip = this.$t("commHandle.editSuccTip");
                 this.$message.success(editSuccTip);
            done();
            this.handleList();
        }).catch(() => {
                loading();
        })
        },
        /**
         * @title 数据删除
         * @param row 为当前的数据
         * @param index 为当前更新数据的行数
         **/
        handleDel (row, index) {
            this.$confirm(this.$t("commHandle.deleteConformTip"), this.$t("commHandle.tip"), {
                confirmButtonText: this.$t("commHandle.conformBtnTxt"),
                cancelButtonText: this.$t("commHandle.cancelBtnTxt"),
                type: 'warning',
            }).then(() => {
                delAntStock(row.id).then(() => {
                    let deleteSuccTip = this.$t("commHandle.deleteSuccTip");
                    this.$message.success(deleteSuccTip);
                    console.log('删除成功==>' + index)
            // done();
            // this.tableData.splice(index, 1);
            this.handleList();
        }).catch(() => {
            })
        })
        .catch(() => { });
        },

        handleRowDel () {
            let row = this.getSelectOneData();
            if(!row)return;
            this.handleDel(row, -1);
        },

        /**公共方法开始**/
        handlerefreshChange (pageBeanModel) {
            console.info(pageBeanModel);
            this.handleList();
        },
        handlePageCurrentChange (currentPage) {
            this.pageBeanModel.currentPage = currentPage;
            this.handleList();
        },
        handlePageSizeChange (pageSize) {
            this.pageBeanModel.pageSize = pageSize;
            this.handleList();
        },
        handleSearchChange (form) {
            this.tableSearch = form;
            this.handleList();
        },
        handleSelectionChange (val) {
            this.tableRow = val;
        },
        /**
         * @获取选择的一行数据
         */
        getSelectOneData(){
            if (validatenull(this.tableRow)) {
                this.$notify({
                    showClose: true,
                    message: this.$t("commHandle.errTipNeedSelectRow"),
                    type: 'error',
                });
                return false;
            } else if (this.tableRow.length > 1) {
                this.$notify({
                    showClose: true,
                    message: this.$t("commHandle.errTipSelectManyRow"),
                    type: 'error',
                });
                return false;
            }
            return this.tableRow[0];
        },
        formatJson (filterVal, jsonData) {
            return jsonData.map(v =>
                    filterVal.map(j => {
                        if (j === 'timestamp') {
                return parseTime(v[j]);
            } else {
                return v[j];
            }
        }),
        );
        },
        tableRowClassName ({ rowIndex }) {
            if (rowIndex === 1) {
                return 'warning-row';
            } else if (rowIndex === 3) {
                return 'success-row';
            }
            return '';
        },
        tip (msg) {
            this.$notify({
                message: msg || '点击事件',
            });
        },
        /**公共方法结束**/
    },
    };
</script>
<style lang="scss">
    @import "@/styles/system.scss";
</style>